﻿using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace StoCombatLogHUD.Configuration
{
	/// <summary>
	/// 
	/// </summary>
	public class MainConfiguration
	{
		/// <summary>
		/// The config file path
		/// </summary>
		private const string ConfigFilePath = @"Content/Configuration.xml";

		/// <summary>
		/// 
		/// </summary>
		private enum Settings
		{
			/// <summary>
			/// The combat log path
			/// </summary>
			CombatLogPath
		}

		/// <summary>
		/// Gets the combat log path.
		/// </summary>
		/// <value>
		/// The combat log path.
		/// </value>
		public string CombatLogPath { get { return GetSettingsValue(Settings.CombatLogPath); } }

		/// <summary>
		/// Gets the value of a specified setting from the configuration file.
		/// </summary>
		/// <param name="setting">The setting.</param>
		/// <returns></returns>
		/// <exception cref="System.IO.FileNotFoundException"></exception>
		private static string GetSettingsValue(Settings setting)
		{
			var configFile = XDocument.Load(ConfigFilePath);
			if (configFile.Root == null)
				throw new FileNotFoundException("Cannot find configuration file");

			var settingsElement = configFile.Root.Descendants().SingleOrDefault(element => element.Name == setting.ToString());
			if (settingsElement == null)
				throw new FileNotFoundException(string.Format("Could not find setting \"{0}\" inside configuration file.", setting));

			return settingsElement.Value;
		}
	}
}
